<?php
/**
 * 库存总表（站点库存）
 * (model)
 */

namespace App\Models\Inventory;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class InventoryTotalArea extends Model{

    protected $table = 'inventory_total_area';

    protected $guarded = [];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [];
            $temp['area'] = $row->area->area ?? '';
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            $temp['classify_title'] = $row->sku->classify->title ?? '';

            $temp['public_instant'] = $row->public_instant ?? 0;
            $temp['public_transit'] = $row->public_transit ?? 0;
            $temp['public_transit'] = $row->public_transit ?? 0;

            $temp['cn_instant'] = $row->cn_instant ?? 0;
            $temp['cn_transit'] = $row->cn_transit ?? 0;
            $temp['cn_reserve'] = $row->cn_reserve ?? 0;

            $temp['fba_instant'] = $row->fba_instant ?? 0;
            $temp['fba_transit'] = $row->fba_transit ?? 0;

            $temp['os_transit'] = ($row->os_box_instant ?? 0) + ($row->os_removale_instant ?? 0) + ($row->os_dropshipping_instant ?? 0);
            $temp['os_transit'] = ($row->os_box_transit ?? 0) + ($row->os_removale_transit ?? 0) + ($row->os_dropshipping_transit ?? 0);
            $temp['os_reserve'] = ($row->os_box_reserve ?? 0) + ($row->os_removale_reserve ?? 0) + ($row->os_dropshipping_reserve ?? 0);

            $temp['os_box_instant'] = $row->os_box_instant ?? 0;
            $temp['os_box_transit'] = $row->os_box_transit ?? 0;
            $temp['os_box_reserve'] = $row->os_box_reserve ?? 0;

            $temp['os_removale_instant'] = $row->os_removale_instant ?? 0;
            $temp['os_removale_transit'] = $row->os_removale_transit ?? 0;
            $temp['os_removale_reserve'] = $row->os_removale_reserve ?? 0;

            $temp['os_dropshipping_instant'] = $row->os_dropshipping_instant ?? 0;
            $temp['os_dropshipping_transit'] = $row->os_dropshipping_transit ?? 0;
            $temp['os_dropshipping_reserve'] = $row->os_dropshipping_reserve ?? 0;

            yield $temp;
        }
    }

    /**
     * 站点库存（汇总）
     * @param $areaId
     * @param $pSkuId
     * @param int $classifyId
     */
    public static function addInventoryTotalArea($areaId, $pSkuId, $classifyId = 0){
        InventoryTotalArea::query()->updateOrCreate(
            [
                'area_id' => $areaId,
                'psku_id' => $pSkuId
            ],
            [
                'classify_id' => $classifyId,
            ]
        );
    }

}
